<?php

namespace apiend\modules\shop\controllers;

use Yii;
use apiend\components\Controller;
use apiend\modules\shop\models\SkuGoodsSearch;
use apiend\modules\shop\models\StandardProductUnit;
use common\helpers\ArrayHelper;
use common\models\shop\ShopCart;
use common\models\views\ViewSkuSpec;

/**
 * SPU相关接口
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class SpuController extends Controller
{

    public function actionFindsku($product_id, $goods_id = [])
    {
        $list = [];
        $currskuinfo = [];
        $where = [];
        $where['product_id'] = $product_id;
        if ($goods_id) {
            $where['goods_id'] = $goods_id;
            $currskuinfo = ViewSkuSpec::find()->where($where)->all();
        } else {
            $currskuinfo = ViewSkuSpec::find()->where($where)->groupBy('spec_id')->all();
        }
        if ($currskuinfo) {
            foreach ($currskuinfo as $value) {
                $data = [];
                $data['id'] = $value['spec_id'];
                $data['name'] = $value['spec_name'];
                if ($goods_id) {
                    $data['checktag'] = $value['tag'];
                } else {
                    $data['checktag'] = 0;
                }
                $skulist = ViewSkuSpec::find()->where(['product_id' => $product_id, 'spec_id' => $value['spec_id']])->groupBy('spec_value')->all();
                $data['skulist'] = $skulist;
                $list[] = $data;
            }
        }
        return $list;
    }

    public function actionSelectsku($product_id, $goodsids = [], $skuid)
    {
        $currgoodsinfo = [];
        if ($goodsids) {
            $id_array = explode(',', $goodsids);
            $currgoodsinfo = ViewSkuSpec::find()->andWhere([
                'product_id' => $product_id,
                'spec_value_id' => $skuid,
                'goods_id' => $id_array,
            ])->all();
        } else {
            $currgoodsinfo = ViewSkuSpec::find()->andWhere([
                'product_id' => $product_id, 'spec_value_id' => $skuid,
            ])->all();
        }
        return $currgoodsinfo;
    }

    public function actionChangesku()
    {
        $model = new SkuGoodsSearch();
        $model->load(Yii::$app->request->getQueryParams(), '');
        return $model->search();
    }

    /**
     * SKU详情
     * @view view.php
     * @return mixed
     */
    public function actionView($id)
    {
        $product = StandardProductUnit::findOrFail($id);
        $skus = ArrayHelper::toArray($product->goods);
        $specs = ArrayHelper::toArray($product->specs);
        $sku = [
            'tree' => $this->formatTree($specs),
            'list' => $skus,
            'price' => $product->price,
            'stock_num' => 227,
            'collection_id' => 1,
            'collection_price' => 0,
            'none_sku' => false,
            'sold_num' => 0,
            'min_price' => '1.00',
            'max_price' => '2.00',
            'hide_stock' => false,
        ];
        $initSku = [];
        if (!empty($skus)) {
            $initSku = array_shift($skus);
        }

        $cartNums = 0;
        if ($this->uid) {
            $cartNums = ShopCart::find()->where([
                'user_id' => $this->uid,
                'deleted_at' => 0,
            ])->sum('num');
        }

        return [
            'goodsinfo' => $product,
            'kdt_id' => 55,
            'user_id' => $this->uid,
            'offline_id' => 0,
            'activity_alias' => '',
            'sku' => $sku,
            'goods_id' => $id,
            'alias' => '2oml0r0n5vytj',
            'quota' => 0,
            'is_virtual' => 0,
            'quota_used' => 0,
            'initialSku' => $initSku,
            'cartSkuCount' => $cartNums ?: '',
        ];
    }

    public function formatTree($specs)
    {
        $trees = [];
        foreach ($specs as $spec) {
            $id = ArrayHelper::remove($spec, 'id');
            $name = ArrayHelper::remove($spec, 'name');
            $imgUrl = ArrayHelper::remove($spec, 'imgUrl');
            $v = compact('id', 'name');
            if ($imgUrl) {
                $v['imgUrl'] = $imgUrl;
            }
            $kid = $spec['k_id'];
            if (!isset($trees[$kid])) {
                $trees[$kid] = $spec;
                $trees[$kid]['count'] = 0;
            }
            $trees[$kid]['v'][$id] = $v;
        }
        foreach ($trees as $key => $item) {
            $v = ArrayHelper::remove($item, 'v');
            $trees[$key]['count'] = count($v);
            $trees[$key]['v'] = array_values($v);
        }
        return array_values($trees);
    }

}
